﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.Dinasty.BO;
using System.Drawing.Drawing2D;

namespace GMS.BasicControls
{
    public partial class FormatedValueColumn : DataGridViewColumn
    {
        public FormatedValueColumn()
            : base()
        {
            this.CellTemplate = new myCell();
        }
    }
    public partial class myCell : DataGridViewTextBoxCell
    {
        public myCell()
            : base()
        {

        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            if (value != null && value.ToString() != string.Empty)
            {
                return new Good(value.ToString()).ToString();
            }
            else
                return string.Empty;
        }
        protected override void OnMouseEnter(int rowIndex)
        {
            base.OnMouseEnter(rowIndex);
            this.DataGridView.InvalidateRow(rowIndex);
        }
        protected override void OnMouseLeave(int rowIndex)
        {
            base.OnMouseLeave(rowIndex);
            this.DataGridView.InvalidateRow(rowIndex);
        }
        GraphicsPath gp;
        bool drawRec;
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            Point pt = this.DataGridView.PointToClient(Control.MousePosition);
            gp = new GraphicsPath();

            drawRec = false;
            if (value != null && value.ToString() != string.Empty && cellBounds.Contains(pt) && (new Good(value.ToString()).GroupGood == 1 || new Good(value.ToString()).GroupGood == 2))
            {
                gp = DrawRoundRect(graphics, new Pen(Brushes.Black), cellBounds.Right - cellBounds.Height - 1, cellBounds.Top + 1, cellBounds.Height - 2, cellBounds.Height - 4, 4);

                drawRec = true;
                Color startColor = Color.White;
                Color endColor = Color.Black;
                Color c1 = Color.FromArgb(150, startColor);
                Color c2 = Color.FromArgb(150, endColor);

                System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(gp.GetBounds(), c1, c2, 90);
                System.Drawing.Drawing2D.LinearGradientBrush c = new System.Drawing.Drawing2D.LinearGradientBrush(gp.GetBounds(), c2, c1, 90);

                if (gp.GetBounds().Contains(pt))
                {
                    graphics.FillPath(b, gp);
                }
                else
                {
                    graphics.FillPath(c, gp);
                }
            }
        }
        public GraphicsPath DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
        {

            GraphicsPath gp = new GraphicsPath();
            gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
            gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
            gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
            gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
            gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
            gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
            gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
            gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);


            gp.CloseFigure();

            g.DrawPath(p, gp);
            return gp;

        }
        protected override void OnMouseMove(DataGridViewCellMouseEventArgs e)
        {
            this.DataGridView.InvalidateRow(e.RowIndex);
            base.OnMouseMove(e);

            Point pt = this.DataGridView.PointToClient(Control.MousePosition);

            if(gp != null)
            this.DataGridView.Cursor = (gp.GetBounds().Contains(pt) && drawRec) ? Cursors.Hand : Cursors.Default;
        }

    }
}
